package com.github.llyb120.mugen;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassFileLoader implements PluginFileLoader{

    @Overrule
    public boolean match(String extName) {
        return extName.equals("class");
    }

    @Overrule
    public Map<String, ByteClass> analyze(PluginOption option, List<File> files) {
        Map<String,ByteClass> map = new HashMap<>();
        for (File file : files) {
            String className = option.getBasePackage() + file.getAbsolutePath().replace(option.getPluginDir().getAbsolutePath(), "")
                    .replace(".class", "")
                    .replaceAll("\\\\|/", ".");
            byte[] bs = FileUtil.readBytes(file);
            ByteClass byteclass = ByteClass.builder()
                    .className(className)
                    .bytes(bs)
                    .build();
            map.put(byteclass.getClassName(), byteclass);
        }
        return map;
    }


}
